<?php
include 'header.php';
include 'constant.php';
include 'db_cred.php';
?>
<script type=text/javascript>
function validate_input()
{

  var event_name = document.forms["main"]["event_name"].value;
  if (event_name == "")
  {
    alert ("Error : Event Name must not be empty");
    return false;
  }

  var event_dt = document.forms["main"]["event_dt"].value;
  var regexp = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$/;
  if (! regexp.test(event_dt))
  {
    alert ("Error : Event Date is not in the correct format");
    return false;
  }

  var event_reminder_interval = document.forms["main"]["event_reminder_interval"].value;
  if ((event_reminder_interval < 0) ||  (event_reminder_interval >= 60))
  {
    alert ("Error : Reminder must be less than 60 minutes");
    return false;
  }

  var user_phone = document.forms["main"]["user_phone"].value;
  var regexp = /^\d{10}\s$/;
  if (! regexp.test(user_phone))
  {
    alert ("Error : Phone Number must contain 10 digits only");
    return false;
  } 
}
</script>
<?php
if ($_SESSION['authorized_user'] == '')
{
  include 'constant.php';
  print $CONST_ERR_INCORRECT_LOGIN_CRED . "<BR>\n";
  print "<A HREF=\"/\">Back</A>\n";
}
else
{
  if ($_REQUEST['event_name'] != '')
  {
      # event_name not null => trying to create event
       $_SESSION['event_status'] = 'Creating event ' . $_REQUEST['event_name'];
       $query = sprintf ("insert into event (user_name, event_name, event_dt, event_reminder_dt, alert_recipients) values ('%s','%s','%s' - INTERVAL %s HOUR,'%s' - INTERVAL %s MINUTE,'%s')",
                     $_SESSION['authorized_user'],
                     $_REQUEST['event_name'],
                     $_REQUEST['event_dt'], $_SESSION['user_timezone_offset'],
                     $_REQUEST['event_dt'], ($_SESSION['user_timezone_offset'] * 60) + $_REQUEST['event_reminder_interval'],
                     $_REQUEST['alert_recipients']
       );
       $result = mysql_query($query) or ($_SESSION['event_status'] = 'Error : ' . mysql_error());
  }
}
?>
Please complete the following information:
<BR />
<BR />
<FORM NAME="main" ONSUBMIT="return validate_input()">
<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>
<TR><TD>Event Name</TD><TD WIDTH=5 /><TD><INPUT TYPE=TEXT NAME=event_name WIDTH=30 /></TD></TR>
<TR><TD>Event Date (YYYY-MM-DD HH24:MI:SS)</TD><TD /><TD><INPUT TYPE=TEXT NAME=event_dt WIDTH=30 /></TD></TR>
<TR><TD>Reminder (minutes)</TD><TD WIDTH=5 /><TD><INPUT TYPE=TEXT NAME=event_reminder_interval WIDTH=30 VALUE=15 /></TD></TR>
<TR><TD>Running Late SMS Recipients</TD><TD WIDTH=5 /><TD><INPUT TYPE=TEXT NAME=alert_recipients WIDTH=30 /></TD></TR>

<TR><TD><FONT COLOR="RED"><?php print $_SESSION['event_status'];?></FONT></TD></TR>
<TR><TD COLSPAN=3 ALIGN=CENTER><INPUT TYPE=SUBMIT VALUE="Create Event" /><INPUT TYPE=BUTTON ONCLICK="location.href='/'" VALUE="Go to Main Page" />
</TD></TR>
</TABLE>
</FORM>
<BR />
<?php
include 'footer.php';
?>
